package com.github.postsanf.yinian.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.GridView;

public class NoScrollGridView extends GridView {
    public NoScrollGridView(Context context) {
        super(context);
    }

    public NoScrollGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    private OnTouchInvalidPositionListener onTouchInvalidPositionListener;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //先创建一个监听接口，一旦点击了无效区域，便实现onTouchInvalidPosition方法，返回true or false来确认是否消费了这个事件
        if (onTouchInvalidPositionListener != null) {
            if (!isEnabled()) {
                return isClickable() || isLongClickable();
            }
            int motionPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
            if (ev.getAction() == MotionEvent.ACTION_UP && motionPosition == INVALID_POSITION) {
                super.onTouchEvent(ev);
                return onTouchInvalidPositionListener.onTouchInvalidPosition(motionPosition);
            }
        }
        return super.onTouchEvent(ev);
    }

    public void setOnTouchInvalidPositionListener(
            OnTouchInvalidPositionListener onTouchInvalidPositionListener) {
        this.onTouchInvalidPositionListener = onTouchInvalidPositionListener;
    }

    public interface OnTouchInvalidPositionListener {
        public boolean onTouchInvalidPosition(int motionEvent);
    }

//    public interface OnTouchInvalidPositionListener {
//        /**
//         * motionEvent 可使用 MotionEvent.ACTION_DOWN 或者 MotionEvent.ACTION_UP等来按需要进行判断
//         *
//         * @return 是否要终止事件的路由
//         */
//        boolean onTouchInvalidPosition(int motionEvent);
//    }
//
//    /**
//     * 点击空白区域时的响应和处理接口
//     */
//    public void setOnTouchInvalidPositionListener(OnTouchInvalidPositionListener listener) {
//        mTouchInvalidPosListener = listener;
//    }
//
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        if (mTouchInvalidPosListener == null) {
//            return super.onTouchEvent(event);
//        }
//        if (!isEnabled()) {
//            // A disabled view that is clickable still consumes the touch
//            // events, it just doesn't respond to them.
//            return isClickable() || isLongClickable();
//        }
//        final int motionPosition = pointToPosition((int) event.getX(), (int) event.getY());
//        if (motionPosition == INVALID_POSITION) {
//            super.onTouchEvent(event);
//            return mTouchInvalidPosListener.onTouchInvalidPosition(event.getActionMasked());
//        }
//        return super.onTouchEvent(event);
//    }
}
